package cn.com.chengmandian.bike.service.impl;

import cn.com.chengmandian.bike.constants.WorkMoveRideResultEnum;
import cn.com.chengmandian.bike.model.EbRideOrder;
import cn.com.chengmandian.bike.model.EbWorkMove;
import cn.com.chengmandian.bike.mapper.EbWorkMoveMapper;
import cn.com.chengmandian.bike.service.IEbWorkMoveService;
import cn.com.chengmandian.core.common.constants.CommonConstant;
import cn.com.chengmandian.core.redis.cache.AgentParamCacheService;
import cn.com.chengmandian.core.web.pojo.BikeAreaParam;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 电单车运营-工作台-挪车记录 服务实现类
 * </p>
 *
 * @author AutoGenerator
 * @since 2023-05-23
 */
@Slf4j
@Service
public class EbWorkMoveServiceImpl extends ServiceImpl<EbWorkMoveMapper, EbWorkMove> implements IEbWorkMoveService {

    @Autowired
    private AgentParamCacheService agentParamCacheService;

    @Override
    public void addRideOrder(EbRideOrder ebRideOrder,Integer agentId) {
        try {
            BikeAreaParam bikeAreaParam = agentParamCacheService.getParam(agentId, ebRideOrder.getAreaId());

            // 产单后查找此车辆是否存在“等待确认”的挪车工单，如果有并且是x小时内挪车，则为有效挪车。
            List<EbWorkMove> ebWorkMoves = this.list(new LambdaQueryWrapper<EbWorkMove>()
                    .eq(EbWorkMove::getBikeNo,ebRideOrder.getBikeNo())
                    .eq(EbWorkMove::getAreaId,ebRideOrder.getAreaId())
                    .eq(EbWorkMove::getStatus, CommonConstant.TWO)
                    .eq(EbWorkMove::getRideResult, WorkMoveRideResultEnum.WAIT.getCode()));
            if(ArrayUtil.isAllEmpty(ebWorkMoves)) return;

            List<EbWorkMove> updateWorkMoves = ebWorkMoves.stream().filter(item -> ObjectUtil.isNotNull(item.getEndTime()) && DateUtil.between(item.getEndTime(), DateUtil.date(), DateUnit.HOUR) <= bikeAreaParam.getOpsWorkMoveHours())
                    .map(item ->{
                        EbWorkMove ebWorkMove = new EbWorkMove();
                        ebWorkMove.setId(item.getId());
                        ebWorkMove.setRideResult(WorkMoveRideResultEnum.DISPATCH_YES.getCode());
                        ebWorkMove.setRideOrderId(ebRideOrder.getId());
                        ebWorkMove.setRideOrderNo(ebRideOrder.getOrderNo());
                        ebWorkMove.setRideOrderTime(ebRideOrder.getCreateTime());
                        return ebWorkMove;
                    }).collect(Collectors.toList());
            if(ArrayUtil.isAllEmpty(updateWorkMoves)) return;
            this.updateBatchById(updateWorkMoves);
        }catch (Exception ex){
            log.error("运维挪车有效性更新失败:{}",ex.getMessage());
        }
    }

    public static void main(String[] args){
        System.out.println(DateUtil.between(DateUtil.date(), DateUtil.date(), DateUnit.HOUR));
    }
}
